package com.turing.post.report.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.MerchantBusinessDaySettlement;
import com.turing.post.report.util.MerchantBusinessDaySettlementDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "report.impl.MerchantBusinessDaySettlementDaoImpl")
@Remote
@Local
public class MerchantBusinessDaySettlementDaoImpl implements
		MerchantBusinessDaySettlementDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insert(MerchantBusinessDaySettlement mds)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, mds);
	}

	@Override
	public void removeAllMerchantBusinessDaySettlementByTranDate(
			String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery("delete MerchantBusinessDaySettlement t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}
	
	/**
	 * 更新商户日结数据
	 * @param mds
	 * @throws PCPSApplicationException
	 */
	public void update(MerchantBusinessDaySettlement mb)
			throws PCPSApplicationException{
		GeneralDao.update(_em, mb);
	}
	
	
	/**
	 * 某商户某日的按业务类型获取汇总数据集合
	 * @param trandate
	 * @param merchantno
	 * @return 某商户某日的按业务类型的汇总数据集合
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<MerchantBusinessDaySettlement> getMerchantBusinessDaySettlementByTrandateAndMerchantno(
			Date trandate, String merchantno) {
		String sql = "select * from MerchantBusinessDaySettlement where merchantno=? and trandate=? order by businesstype";
	Query query = _em.createNativeQuery(sql, MerchantBusinessDaySettlement.class);
	query.setParameter(1, merchantno);
	query.setParameter(2, trandate);
	return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getMerchantBusinessDaySettlementTotal(
			String issueCompanyNo, String startDate, String endDate,
			String merchantno) {
		String sql = " select m.merchantno,to_char(m.trandate,'yyyy-mm-dd'),m.businesstype,nvl(sum(m.cost)/100,0),nvl(sum(m.income)/100,0)," +
			" nvl(sum(m.consume+m.otherpaid-m.cancelpaid)/100,0),nvl(sum(m.consume-m.cancelpaid)/100,0),nvl(sum(m.otherpaid)/100,0) " +
			" from MerchantBusinessDaySettlement m where m.issuecompanyno = ?1 " +
			" and m.trandate >= to_date(?2,'yyyy-mm-dd') " +
			" and m.trandate <= to_date(?3,'yyyy-mm-dd') " +
			" and m.merchantno in ("+merchantno+") " +
			" group by m.merchantno,m.trandate,m.businesstype order by m.trandate desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, startDate);
		query.setParameter(3, endDate);
		List<Object[]> obj_list = query.getResultList();
		return obj_list;
	}
}